package core1.ch14thread;
public class CalculatePrimes extends Thread {

	private static int MAX_PRIMES = 100;

	private static int TEN_SECONDS = 10000;

	public volatile boolean finished = false;

	/* (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	public void run() {
		int[] primes = new int[MAX_PRIMES];
		int count = 0;

		for (int i = 1; count < MAX_PRIMES; i++) {
			if (finished) {
				break;
			}
			boolean isPrime = true;
			
			for (int j = 2; j < Math.sqrt(Double.parseDouble(Integer.toString(i)))+1; j++) {
				if(i%j==0){
					isPrime=false;
					break;
				}
			}
			if (isPrime) {
				primes[count++] = i;
				System.out.println("Found prime :" + i);
			}
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CalculatePrimes calculator = new CalculatePrimes();
		calculator.start();

		try {
			Thread.sleep(TEN_SECONDS);

		} catch (InterruptedException ex) {
			ex.printStackTrace();
		}
		calculator.finished = true;
	}

}
